package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.common.LoanApplication;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.ui.VCodeButton;
import com.hjwoo.app.utils.JsonParser;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

/**
 * Created by yangchuan on 2016/5/20.
 */
public class RetrievePasswordActivity extends BaseAppCompatActivity implements RequestListener
{
    private EditText phone, vcode;
    private Button next;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrievepassword);
        init();
    }

    private void init()
    {
        Utils.initTitle("找回密码", RetrievePasswordActivity.this, true);
        phone = (EditText) findViewById(R.id.retrieve_phone_et);
        vcode = (EditText) findViewById(R.id.retrieve_vcode_et);
        next = (Button) findViewById(R.id.retrieve_next_btn);
        final VCodeButton vCodeButton = (VCodeButton) findViewById(R.id.retriva_vcode_btn);

        vCodeButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String phonenumber = phone.getText().toString().trim();
                if (TextUtils.isEmpty(phonenumber))
                {
                    Toast.makeText(RetrievePasswordActivity.this, "请输入您的电话号码!", Toast.LENGTH_SHORT).show();
                    return;
                }
                RequestMethod.getMethod().appSendVerifiedCode(phonenumber, "UpdatePassword", RetrievePasswordActivity.this, RetrievePasswordActivity.this);
                vCodeButton.startCount();
            }
        });
        next.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                String vcodenumber = vcode.getText().toString().trim();
                if (TextUtils.isEmpty(phone.getText().toString().trim()))
                {
                    Toast.makeText(RetrievePasswordActivity.this, "请输入您的电话号码!", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(vcodenumber))
                {
                    Toast.makeText(RetrievePasswordActivity.this, "请输入验证码!", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(RetrievePasswordActivity.this, ResetpasswordActivity.class);
                intent.putExtra("cellphone", phone.getText().toString().trim());
                intent.putExtra("vcode", vcode.getText().toString().trim());
                startActivity(intent);
            }
        });
    }

    @Override
    public void requestSuccess(String tag, String message)
    {
        if (tag.equals(RequestTag.APPSENDVERIFIEDCODE))
        {
            Log.e("appSendVerifiedCode", message);
        }
    }

    @Override
    public void requestError(String message)
    {
        FailureResultObject result = JsonParser.getJsonParser().parserJsonToObj(message, FailureResultObject.class);
        LoanToast.showToastShort(result.getErrorMessage(), LoanApplication.getContext());
    }
}
 
